package com.dz.risk.controller;

import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.enums.CodeEnum;
import com.dz.risk.common.exception.RRException;
import com.dz.risk.pojo.ParamsVo;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.TableDictionaryEntity;
import com.dz.risk.service.CommonService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author baohan
 * @className ComController
 * @description TODO
 * @date 2021/6/29 10:46
 */
@RestController
@RequestMapping("dz-ftsp/stest/comm")
@Api(tags = "公共方法api", value = "公共方法api接口")
@Slf4j
public class ComController {

    @Autowired
    CommonService commonService;

    /**
     * 编辑公式单元格
     */
    @ApiOperation("编辑公式单元格")
    @PostMapping("updateFormula")
    public ResponseVO updateFormula(@RequestBody ParamsVo paramsVo) {
        try {
            commonService.updateFormula(paramsVo);
        } catch (RRException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return new ResponseVO(e.getMessage());
        }
        return new ResponseVO(CodeEnum.SUCCESS);
    }

    /**
     * 查看表层级关系
     */
    @ApiOperation("查看表层级关系")
    @PostMapping("getTableAll")
    public ResponseVO getTableAll() {
        List<TableDictionaryEntity> tableAll = null;
        try {
            tableAll = commonService.getTableAll();
        } catch (RRException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return new ResponseVO(e.getMessage());
        }
        return new ResponseVO(tableAll);
    }

    /**
     * 查看表层级关系
     */
    @ApiOperation("根据表名所在表信息详情")
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping(value = "getTableNameByList",
            produces = {"application/json", "text/xml"})
    public ResponseVO getTableNameByList(@RequestBody QueryParams queryParams) {
        PageInfo<Map<String, Object>> list = null;
        try {
            list = commonService.getTableNameByList(queryParams);
        } catch (RRException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return new ResponseVO(e.getMessage());
        }
        return new ResponseVO(list);
    }

}
